package com.von.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.von.entity.User;
import com.von.service.IUserService;
import com.baomidou.mybatisplus.extension.api.R;
import com.von.utils.TokenUtils;
import com.von.vo.RoleVo;
import com.von.vo.UserPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author absorprofess
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * @apiDefine user 用户管理
     */

    /**
     * @apiDefine SuccessResponse
     * @apiSuccess (成功) {int} status 状态码
     * @apiSuccess (成功) {String} error 错误信息
     * @apiSuccess (成功) {Object} data 数据
     */

    /**
     * @apiDefine ErrorResponse
     * @apiError (错误) {int} status  错误码
     * @apiError (错误) {Object} data
     * @apiError (错误) {String} error  错误信息
     * @apiErrorExample {json} Error-Response:
     * {
     * "status":-1,
     * "data":null,
     * "error":"错误信息"
     * }
     */

    @Autowired
    private IUserService iUserService;

    @GetMapping("/byToken")
    public R<Map> byToken() {
        return iUserService.byToken();
    }

    @PostMapping("/updateRole")
    public R<String> updateRole(@RequestBody RoleVo roleVo) {
        return iUserService.updateRole(roleVo);
    }

    /**
     * @api {post} /user/addUser 用户添加
     * @apiGroup user
     * @apiHeader {String} token token
     * @apiParam {String} name 用户名
     * @apiParam {String} phone 手机号
     * @apiParam {String} account 账号
     * @apiParam {String} password 密码
     * @apiParamExample {json} Request-Example:
     * {
     * "name":"dataDemo",
     * "phone":"dataDemo",
     * "account":"dataDemo",
     * "password":"dataDemo"
     * }
     * @apiUse SuccessResponse
     * @apiSuccessExample {json} success-example:
     * {
     * "code":0,
     * "data":"",
     * "msg":""
     * }
     */
    @PostMapping("/addUser")
    public R<String> addUser(@RequestBody User user) {
        return iUserService.insert(user);
    }

    /**
     * @api {post} /user/updateUser 用户添加
     * @apiGroup user
     * @apiHeader {String} token token
     * @apiParam {String} name 用户名
     * @apiParam {String} phone 手机号
     * @apiParam {String} account 账号
     * @apiParam {String} password 密码
     * @apiParamExample {json} Request-Example:
     * {
     * "name":"dataDemo",
     * "phone":"dataDemo",
     * "account":"dataDemo",
     * "password":"dataDemo"
     * }
     * @apiUse SuccessResponse
     * @apiSuccessExample {json} success-example:
     * {
     * "code":0,
     * "data":"",
     * "msg":""
     * }
     */
    @PostMapping("/updateUser")
    public R<String> updateUser(@RequestBody User user) {
        return iUserService.update(user);
    }

    /**
     * @api {post} /user/deleteUser 用户删除
     * @apiGroup user
     * @apiHeader {String} token token
     * @apiParam {Integer} id id
     * @apiUse SuccessResponse
     * @apiSuccessExample {json} success-example:
     * {
     * "code":0,
     * "data":"",
     * "msg":""
     * }
     */
    @GetMapping("/deleteUser")
    public R<String> deleteUser(Integer id) {
        return iUserService.deleteById(id);
    }

    /**
     * @api {post} /user/list 用户列表
     * @apiGroup user
     * @apiHeader {String} token token
     * @apiParam {String} pageNum 当前页
     * @apiParam {String} pageSize 页面条数
     * @apiParam {String} name 用户名
     * @apiParam {String} phone 手机号
     * @apiParam {String} account 账号
     * @apiParamExample {json} Request-Example:
     * {
     * "pageNum":1,
     * "pageSize":10,
     * "name":"dataDemo",
     * "phone":"dataDemo",
     * "account":"dataDemo",
     * "password":"dataDemo"
     * }
     * @apiUse SuccessResponse
     * @apiSuccessExample {json} success-example:
     * {
     *     "code": 0,
     *     "data": {
     *         "records": [
     *             {
     *                 "id": 9,
     *                 "name": "杭州",
     *                 "phone": "18186102014",
     *                 "account": "杭州",
     *                 "password": "123456",
     *                 "createTime": "2021-03-10 07:46:43",
     *                 "status": "VALID",
     *                 "isdel": false
     *             }
     *         ],
     *         "total": 4,
     *         "size": 10,
     *         "current": 1,
     *         "orders": [],
     *         "hitCount": false,
     *         "searchCount": true,
     *         "pages": 1
     *     },
     *     "msg": "执行成功"
     * }
     */
    @PostMapping("/list")
    public R<Page<User>> listUser(@RequestBody UserPage userPage){
        return iUserService.list(userPage);
    }
}
